package com.atguigu.juc1205.fileoperation;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.joda.time.DateTime;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author kfzx-shimy01
 * @date 2019/7/30
 */
public class DateUtil extends org.apache.commons.lang3.time.DateUtils {

    /**
     * 得到当前日期和时间字符串 格式（yyyy-MM-dd）
     */
    public static String getDateTime(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, Object... pattern) {
        String formatDate = null;
        if (null != pattern && pattern.length > 0) {
            formatDate = DateFormatUtils.format(date, pattern[0].toString());
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }

    public static String getDay(Date time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = dateFormat.format(time);
        return date;
    }

    public static Integer getMonth(Date time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        String date = dateFormat.format(time);
        return Integer.parseInt(date);
    }

    /**
     * 对日期的【天】进行加/减
     *
     * @param date 日期
     * @param days 天数，负数为减
     * @return 加/减几天后的日期
     */
    public static Date addDateDays(Date date, int days) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(days).toDate();
    }

    /**
     * date2 比 date1多的天数，精确度为天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        //不同年
        if (year1 != year2) {
            return daysOfDifferentYear(day1, day2, year1, year2);
        }
        return day2 - day1;
    }

    /**
     * date2 比 date1多的天数，精确度为天
     *
     * @param rangeDate 时间范围
     * @return
     */
    public static int differentDaysByRange(RangeDate rangeDate) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(rangeDate.getBeginDate());

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(rangeDate.getEndDate());
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        //不同年
        if (year1 != year2) {
            return daysOfDifferentYear(day1, day2, year1, year2);
        }
        return day2 - day1;
    }

    /**
     *
     * @param day1
     * @param day2
     * @param year1
     * @param year2
     * @return
     */
    private static int daysOfDifferentYear(int day1, int day2, int year1, int year2) {
        int timeDistance = 0;
        for (int i = year1; i < year2; i++) {
            //闰年
            if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
                timeDistance += 366;
            } else {
                timeDistance += 365;
            }
        }
        return timeDistance + (day2 - day1);
    }

    /**
     * date 是否在当前日期之后
     *
     * @param date
     * @return
     */
    public static boolean isDayAfter(Date date) {
        return differentDays(new Date(), date) > 0;
    }

    /**
     * date 给定日期是否在指定范围内
     * 例如 输入 2021-05-07 365
     * 2021-05-07 是否包含在 365 天前到今天的范围
     *
     * @param date
     * @return
     */
    public static boolean inRangeDay(Date date,int range) {
        return differentDays(date, new Date()) < range;
    }

    public static void main(String[] args) {
        Date sevenDayAgo = DateUtil.addDateDays(new Date(), -6);
        System.out.println(formatDate(sevenDayAgo));
        Date day = DateUtil.addDateDays(new Date(), -6);
        System.out.println(formatDate(day));
        System.out.println(inRangeDay(day,7));
    }
}
